using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public static class DllCofingHelper
{
#if UNITY_EDITOR
    [UnityEditor.MenuItem("Dll/Copy")]
    public static void CopyDll()
    {
        var assets = UnityEditor.AssetDatabase.FindAssets("t:DllConfig");
        foreach(var guid in assets)
        {
            var path = UnityEditor.AssetDatabase.GUIDToAssetPath(guid);
            DllConfig cfg = UnityEditor.AssetDatabase.LoadAssetAtPath<DllConfig>(path);
            cfg.CopyDll();
        }
    }
#endif
}

[CreateAssetMenu(fileName = "DllConfigSettings", menuName = "Create DllConfig Settings")]
public class DllConfig : ScriptableObject
{
    public string[] hotFixDlls = new string[0];
    public string[] aotDlls = new string[0];

#if UNITY_EDITOR
    public void CopyDll()
    {
        foreach (var dll in hotFixDlls)
        {
            var srcPath = $"HybridCLRData/HotUpdateDlls/{UnityEditor.EditorUserBuildSettings.activeBuildTarget}/{dll}";
            var dstPath = $"Assets/Space Shooter/GameRes/Dlls/{dll}.bytes";
            var dir = Path.GetDirectoryName(dstPath);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            File.Copy(srcPath, dstPath, true);
        }
        //Space Shooter
        foreach (var dll in aotDlls)
        {
            var srcPath = $"HybridCLRData/AssembliesPostIl2CppStrip/{UnityEditor.EditorUserBuildSettings.activeBuildTarget}/{dll}";
            var dstPath = $"Assets/Space Shooter/GameRes/Dlls/{dll}.bytes";
            var dir = Path.GetDirectoryName(dstPath);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            File.Copy(srcPath, dstPath, true);
        }
        UnityEditor.AssetDatabase.Refresh();
        Debug.Log("Copy Dll Finished");
    }
#endif
}
